package com.gavindon.android.widget;

import android.os.Build;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;

public class FocusViewUtils {
    private static final float scale0 = 1f;
    private static final float scaleL = 1.1f;
    private static final float scaleS = 1.13f;
    private static final float scaleSS = 1.05f;
    private static final float z1 = 0f;
    private static final float z2 = 1f;
    private static final long animateDuration = 200;


    /**
     * view获取焦点时放大/缩小
     *
     * @param v    焦点view
     * @param type 焦点状态view放大的类型
     */
    public static void scaleView(View v, final int type) {
        v.setOnFocusChangeListener((v1, hasFocus) -> {
            //初始化放大倍数
            float scaleX1, scaleX2, scaleY1, scaleY2;
            scaleX1 = scaleY1 = scaleX2 = scaleY2 = scale0;
            if (type == 0) {
                //类型为0放大倍数为1.1f
                scaleX2 = scaleY2 = scaleL;
            }
            if (type == 1) {
                //类型为1放大倍数为1.13f
                scaleX2 = scaleY2 = scaleS;
            }
            if (type == 2) {
                scaleX2 = scaleY2 = scaleSS;
            }
            //获得焦点时的状态和倍数
            if (hasFocus) {

                //在android5.0以上的效果
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    v1.animate()
                            .scaleXBy(scaleX1)
                            .scaleX(scaleX2)
                            .scaleYBy(scaleY1)
                            .scaleY(scaleY2)
                            .zBy(z1)
                            .z(z2)
                            .setInterpolator(new AccelerateDecelerateInterpolator())
                            .setDuration(animateDuration);
                } else {
                    //在android5.0以下的效果
                    v1.animate()
                            .scaleXBy(scaleX1)
                            .scaleX(scaleX2)
                            .scaleYBy(scaleY1)
                            .scaleY(scaleY2)
                            .withEndAction(new Runnable() {
                                @Override
                                public void run() {
                                    //防止view被遮挡
                                    v1.bringToFront();
                                }
                            })
                            .setDuration(animateDuration);
                }
            } else {
                //view在焦点消失后的倍数和状态
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    v1.animate()
                            .scaleXBy(scaleX2)
                            .scaleX(scaleX1)
                            .scaleYBy(scaleY2)
                            .scaleY(scaleY1)
                            .zBy(z2)
                            .z(z1)
                            .setDuration(animateDuration);
                } else {
                    v1.animate()
                            .scaleXBy(scaleX2)
                            .scaleX(scaleX1)
                            .scaleYBy(scaleY2)
                            .scaleY(scaleY1)
                            .setDuration(animateDuration);
                }
            }
        });
    }
}

